﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiXuLong.Model;

namespace ShiXuLong.Service
{

    public class FrontDesk
    {
        public bool Login(string un, string pwd, out int UserId)
        {
            try
            {
                var user = db.Users.Where(p => p.UserName.Equals(un) && p.UserTypeId == 1).SingleOrDefault();

                if (user.UserName.Equals(un) && user.UserPwd == pwd)
                {
                    UserId = user.Id;
                    return true;
                }
                else
                {
                    UserId = 0;
                    return false;
                }
            }
            catch (Exception)
            {
                UserId = 0;
                return false;
            }

        }
        ShoesEntities db = new ShoesEntities();
        /// <summary>
        /// 获取所有的商品类别
        /// </summary>
        /// <returns></returns>
        public List<ShopTypess> GetShopType()
        {
            var list = from item in db.ShopTypes.Where(p => true)
                       select new ShopTypess
                       {
                           Id = item.Id,
                           ShopTypeName = item.ShopTypeName
                       };
            return list.ToList();
        }
        /// <summary>
        /// 获取推荐的商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<RECShop> GetRecShop()
        {
            var list = from c in db.Recommends.Where(p => true)
                       select new RECShop
                       {
                           Id = c.Id,
                           ShopId = c.ShopId,
                           ShopName = c.Shop.ShopName,
                           Shopimage = c.Shop.Shopimage

                       };
            return list.ToList();

        }
        /// <summary>
        /// 删除购物车中的一个商品，根据id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelCartShop(int id)
        {
            var Cart = db.Carts.Where(p => p.Id == id).SingleOrDefault();
            db.Carts.Remove(Cart);
            var res = db.SaveChanges();
            return res;
        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <returns></returns>
        public int ClearCart()
        {
            var Cart = db.Carts.Where(p => true);
            db.Carts.RemoveRange(Cart);
            var res = db.SaveChanges();
            return res;
        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public int PlaceOrder(string orders, string address)
        {
            orders = orders.Substring(0, orders.Length - 1);
            var resToOrderItem = 0;
            var lists = from c in db.Carts
                        where orders.Contains(c.Id.ToString())
                        select new OrderDetail
                        {
                            ShopId = c.Shop.Id,
                            UserId = c.User.Id,
                            OrderId = 1,
                            PayMoney = 1,
                            Freight = 1,
                            Stutas = "0",
                            PayTime = DateTime.Now,
                            SendTime = DateTime.Now,
                            EndTime = DateTime.Now,
                            CreateTime = DateTime.Now,
                            DataTime = DateTime.Now,
                            Address = address,
                            Number = c.ProductNum,
                            Price = c.Shop.Price
                        };

            var CartLists = lists.ToList();
            var totalPrice = 0;
            foreach (var c in CartLists)
            {
                totalPrice += c.Number * Convert.ToInt32(c.Price);
            }
            var list = lists.ToList()[0];
            db.Orders.Add(new Model.Order
            {
                ShopId = list.ShopId,
                UserId = list.UserId,
                OrderId = 1,
                PayMoney = totalPrice,
                Freight = 1,
                PayTime = DateTime.Now,
                SendTime = DateTime.Now,
                EndTime = DateTime.Now,
                CreateTime = DateTime.Now,
                DataTime = DateTime.Now,
                Status = 0,
                EvaluationState = 0,
                TransactionStatus = 0,
                Address = list.Address
            });
            var OrderAddRes = db.SaveChanges();
            if (OrderAddRes > 0)
            {
                var re = from c in db.Carts
                         where orders.Contains(c.Id.ToString())
                         select new OrderDetail1
                         {
                             OrderId = 1,
                             UserId = c.UserId,
                             ProductId = c.ProductId,
                             ProductName = c.Shop.ShopName,
                             Picurl = c.Shop.Shopimage,
                             Price = 1,
                             Number = 1,
                             CreateTime = DateTime.Now,
                             DateTime = DateTime.Now,
                             StandardsId = 1,
                             Status = 0
                         };
                var OrderItemLists = re.ToList();
                if (OrderItemLists != null)
                {
                    var cd2 = db.Orders.Max(p => p.Id);
                    foreach (var item in OrderItemLists)
                    {
                        db.OrderItems.Add(new OrderItem
                        {

                            OrderId = cd2,
                            UserId = item.UserId,
                            ProductId = item.ProductId,
                            ProductName = item.ProductName,
                            Picurl = item.Picurl,
                            Price = 1,
                            Number = 1,
                            CreateTime = DateTime.Now,
                            DateTime = DateTime.Now,
                            StandardsId = 1,
                            Status = 0
                        });
                    }

                }
                resToOrderItem = db.SaveChanges();
                if (resToOrderItem > 0)
                {
                    var cd123 = from c in db.Carts
                                where orders.Contains(c.Id.ToString())
                                select c;

                    db.Carts.RemoveRange(cd123);
                    var delCartDel = db.SaveChanges();
                    if (delCartDel > 0)
                    {
                        return delCartDel;
                    }
                }
            }

            return 0;
        }
        /// <summary>
        /// 获取商品，通过商品类别id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Shop> GetShopByType(int id, out int count)
        {
            var list = db.Shops.Where(p => p.ShopTypeId == id);
            count = list.Count();
            return list.ToList();
        }
        /// <summary>
        /// 获取购物车的数据
        /// </summary>
        /// <returns></returns>
        public List<Cartss> GetCart()
        {
            var list = from item in db.Carts2.Where(p => true)
                       select new Cartss
                       {
                           Id = item.Id,
                           ShopId = item.ProductId,
                           ShopName = item.ShopName,
                           ShopImage = item.Shopimage,
                           Price = item.Price,
                           ShopNum = item.ProductNum,
                           Total = item.Total
                       };
            return list.ToList();
        }
        /// <summary>
        /// 通过id获取商品表的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Shop GetShopById(int id)
        {
            var list = db.Shops.Where(p => p.Id == id);
            return list.ToList().SingleOrDefault();
        }

        public List<Model.OrderTest> GetOrder()
        {
            var lists = from c in db.Orders.Where(p => true)
                        select new OrderTest
                        {
                            Id = c.Id,
                            OrderId = c.Id,
                            Price = c.Shop.Price,
                            UserId = c.UserId,
                            ShopImage = c.Shop.Shopimage,
                            ShopName = c.Shop.ShopName,
                        };

            return lists.ToList();
        }
        public List<Model.OrderTest> GetOrderDetailById(int id = 0)
        {
            var lists = from c in db.Orders.Where(p => p.TransactionStatus == id)
                        select new OrderTest
                        {
                            Id = c.Id,
                            OrderId = c.Id,
                            Price = c.Shop.Price,
                            UserId = c.UserId,
                            ShopImage = c.Shop.Shopimage,
                            ShopName = c.Shop.ShopName,
                        };

            return lists.ToList();
        }
        public List<Model.OrderTest> GetOrderDetailByIdEvaluate(int id = 0)
        {
            var lists = from c in db.Orders.Where(p => p.EvaluationState == id)
                        select new OrderTest
                        {
                            Id = c.Id,
                            OrderId = c.Id,
                            Price = c.Shop.Price,
                            UserId = c.UserId,
                            ShopImage = c.Shop.Shopimage,
                            ShopName = c.Shop.ShopName,
                        };

            return lists.ToList();
        }
        public int UpdateCartsNum(int id, int num)
        {
            var list = db.Carts.Where(p => p.Id == id).ToList().SingleOrDefault();
            list.ProductNum = num;
            var res = db.SaveChanges();
            return res;
        }
        /// <summary>
        /// 根据传过来的id查询相应的数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<Model.OrderDetail1> GetCart(string ids)
        {
            try
            {
                var re = from c in db.Carts
                         where ids.Contains(c.Id.ToString())
                         select new OrderDetail1
                         {
                             Id = c.Id,
                             OrderId = 1,
                             UserId = c.UserId,
                             ProductId = c.ProductId,
                             ProductName = c.Shop.ShopName,
                             Picurl = c.Shop.Shopimage,
                             Price = 1,
                             ShopNum = c.ProductNum,
                             CreateTime = DateTime.Now,
                             DateTime = DateTime.Now,
                             StandardsId = 1,
                             Status = 0
                         };
                return re.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Model.Cartss> GetOrdersById(string CartId)
        {
            try
            {

                var lists = from item in db.Carts2
                            where CartId.Contains(item.Id.ToString())
                            select new Cartss
                            {
                                Id = item.Id,
                                ShopId = item.ProductId,
                                ShopName = item.ShopName,
                                ShopImage = item.Shopimage,
                                Price = item.Price,
                                ShopNum = item.ProductNum,
                                Total = item.Total
                            };
                return lists.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }
        public Address GetAddress(int id)
        {
            try
            {
                var list = db.Addresses.Where(p => p.Id == id).ToList().SingleOrDefault();
                return list;
            }
            catch (Exception)
            {

                return null;
            }
           
        }
    }
}
